import axios from 'axios';
let myPromise = new Promise((resolve, reject) => {
  let protocol = window.location.protocol;
  let host = window.location.host;
  let localUrl = protocol+'//'+host;
  /* 利用Promise函数处理axios请求的base地址，减少部署时的配置需求 */
  axios.get(localUrl+'/src/verAxios.php').then(res=>{
    if(res.data.code == 200){return resolve()}else{return reject()}
  })
})
myPromise.then(()=>{
  axios.defaults.baseURL = localUrl
})
.catch(()=>{
  axios.defaults.baseURL = 'http://192.168.1.104:83';
})
axios.interceptors.request.use(// 请求拦截器
  (config) => {// 在发送请求之前
    config.headers['Content-Type']='application/json;charset=UTF-8'
    return config;
  },
  (error) => {// 请求错误
    return Promise.reject(error);
  }
);
axios.interceptors.response.use(// 响应拦截器
  (response) => {// 响应数据
    return response;
  },
  (error) => {// 响应错误
    return Promise.reject(error);
  }
);
const api = axios;
export default api;